package kit.msc.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import kit.msc.data.Time;
import kit.msc.utility.Preference;
import kit.msc.utility.ScreenSize;

public class SetTimeFrm extends JFrame {

	/**
   *
   */
	private static final long serialVersionUID = 1L;
	private ScreenSize screenSize;
	private JPanel rootPanel;

	public SetTimeFrm() {
		this(null, null);
	}

	SetTimeFrm(Time from, Time to) {
		super(Preference.get("timeViewName", "Set your time!"));
		this.setName(Preference.get("timeViewName", "Set your time!"));

		// set config icon
		Image icon = Toolkit.getDefaultToolkit().getImage("src/main/resources/icon/time.png");
		this.setIconImage(icon);

		rootPanel = new JPanel();

		Border compound;
		Border lineBorder = BorderFactory.createMatteBorder(5, 5, 5, 5, Color.lightGray);
		Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
		compound = BorderFactory.createCompoundBorder(emptyBorder, lineBorder);

		rootPanel.setBorder(compound);
		rootPanel.setBackground(Color.WHITE);

		rootPanel.setLayout(new BorderLayout());

		JLabel timeLabel = new JLabel(Preference.get("timeViewName", "Set your time!"));
		rootPanel.add(timeLabel, BorderLayout.PAGE_START);

		JPanel timePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
		JPanel fromPanel = new JPanel(new FlowLayout());
		JPanel toPanel = new JPanel(new FlowLayout());

		JLabel fromLabel = new JLabel(Preference.get("timeViewFromText", "from:"));
		JTextField fromTime = new JTextField("  :  ");
		if (from != null) {
			fromTime = new JTextField(from.toString());
		}

		fromPanel.add(fromLabel);
		fromPanel.add(fromTime);

		JLabel toLabel = new JLabel(Preference.get("timeViewToText", "to:"));
		JTextField toTime = new JTextField("  :  ");
		if (to != null) {
			toTime = new JTextField(to.toString());
		}
		toPanel.add(toLabel);
		toPanel.add(toTime);

		timePanel.add(fromPanel);
		timePanel.add(toPanel);
		timePanel.setBackground(Color.WHITE);
		rootPanel.add(timePanel, BorderLayout.CENTER);

		JButton timeButton = new JButton(Preference.get("timeViewConfirmButton", "Confirm your time!"));
		rootPanel.add(timeButton, BorderLayout.PAGE_END);

		this.setContentPane(rootPanel);

		this.requestFocus();
		this.setAlwaysOnTop(true);

		screenSize = new ScreenSize();

		this.setMaximumSize(screenSize.getScaledDimension(0.3));
		this.setPreferredSize(screenSize.getScaledDimension(0.3));
		this.setMinimumSize(new Dimension(Preference.getInt("timeViewMinWidth", 300), Preference.getInt("timeViewMinHeight", 150)));

		this.setUndecorated(true);

		pack();
		this.setLocationRelativeTo(null);

		this.setVisible(true);
	}

}
